package com.company.web;

import com.company.core.Result;
import com.company.core.ResultGenerator;
import com.company.model.SysAcl;
import com.company.service.SysAclService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
* Created by CodeGenerator on 2018/11/17.
*/
@RestController
@RequestMapping("/sys/acl")
public class SysAclController {
    @Resource
    private SysAclService sysAclService;


    //@Autowired
    //private RestTemplate restTemplate;


    @PostMapping
    public Result add(@RequestBody SysAcl sysAcl) {
        //ResponseEntity<Object> exchange;
        //exchange = restTemplate.exchange("http://localhost:8080", HttpMethod.POST, null, null,);
        sysAclService.save(sysAcl);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        sysAclService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody SysAcl sysAcl) {
        sysAclService.update(sysAcl);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        SysAcl sysAcl = sysAclService.findById(id);
        return ResultGenerator.genSuccessResult(sysAcl);
    }

    @GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SysAcl> list = sysAclService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
